#!/bin/sh

#attivazioni erronee server
if ./bibserver ; then
    exit 1
fi

if ./bibserver __file__ ; then
    exit 1
fi

if ./bibserver __f1 __f2 __f3; then
    exit 1
fi

# file bib non esiste
if ./bibserver unipi __fi ; then
    exit 1
fi


# attivazioni erronee client
# deve comparire almeno una opzione
if ./bibclient ; then
    exit 1 
fi


# argomenti errati
if ./bibclient pippo ; then
    exit 1
fi

# argomenti errati
if ./bibclient pippo pluto e paperone ; then
    exit 1
fi

# opzione errata (manca argomento)
if ./bibclient --autore ; then
    exit 1
fi

# opzione replicata
if  ./bibclient --autore=ciccio --autore="pluto paperone etc" ; then
    exit 1
fi

# test con singola biblioteca
cat > ./bib.conf <<EOF
scuolanormalepi
EOF

# invocazione bibserver su una sola biblioteca
for FILE in $(cat bib.conf) ; do
    echo Attivo server $FILE
    ./bibserver $FILE $FILE.bib &
done

# i client
./bibclient --titolo=cicciobello --autore="pluto paperone etc"
./bibclient --descrizione_fisica=inc --collocazione="Q."
./bibclient --anno=2011 
./bibclient --anno=2011 --autore=Steel -p
./bibclient --anno=2011 --descrizione_fisica=inc -p
./bibclient --anno=2011 -p
./bibclient --autore=Aho -p
./bibclient --luogo_pubblicazione="Milano" --editore=Jackson --collocazione=Z

# richiedo e attendo la terminazione del server
killall -w bibserver

# controllo il file di log il numero delle query e dei prestiti deve essere lo
# stesso atteso e nella stessa sequenza
echo Controllo file di log ...
tmp=$(mktemp)
tmpcheck=$(mktemp)
for FILE in $(cat bib.conf) ; do
    echo Check QUERY di log $FILE
    grep QUERY $FILE.log > $tmp
    grep QUERY $FILE.log.check > $tmpcheck
    if ! diff $tmp $tmpcheck ; then
	rm $tmp
	rm $tmpcheck
	exit 1
    fi
    echo Check LOAN di log $FILE
    grep LOAN $FILE.log > $tmp
    grep LOAN $FILE.log.check > $tmpcheck
    if ! diff $tmp $tmpcheck ; then
	rm $tmp
	rm $tmpcheck
	exit 1
    fi
done
rm $tmp
rm $tmpcheck

exit 0



